package danny.hoi.service.cargo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import danny.hoi.dao.export.cargo.*;
import danny.hoi.domain.export.cargo.*;
import danny.hoi.service.cargo.ExportService;
import danny.hoi.utils.UuidUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(timeout = 1000000)
public class ExportServiceImpl implements ExportService {
    @Autowired
    private ExportDao exportDao;
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ExportProductDao exportProductDao;
    @Autowired
    private ExtCproductDao extCproductDao;
    @Autowired
    private ExtEproductDao extEproductDao;

    @Override
    public List<Export> findAll(ExportExample example) {
        return exportDao.selectByExample(example);
    }

    @Override
    public PageInfo<Export> findByPages(ExportExample example, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Export> exports = exportDao.selectByExample(example);

        return new PageInfo<>(exports);
    }

    @Override
    public Export findById(String id) {
        return exportDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(Export export) {
        export.setId(UuidUtil.getUuid());
        export.setState(0L);
        //获取contractIds数组
        String[] contractIds = export.getContractIds().split(",");
        StringBuilder customerContract = new StringBuilder();

        for (String contractId : contractIds) {
            Contract contract = contractDao.selectByPrimaryKey(contractId);
            customerContract.append(contract.getContractNo()).append(" ");
            contract.setState(2);
            //更新购销合同状态
            contractDao.updateByPrimaryKeySelective(contract);
        }
        //设置报运单客户号
        export.setCustomerContract(customerContract.toString());

        //查询货物
        ContractProductExample contractProductExample = new ContractProductExample();
        ContractProductExample.Criteria criteria = contractProductExample.createCriteria();
        criteria.andContractIdIn(Arrays.asList(contractIds));
        List<ContractProduct> contractProducts =
                contractProductDao.selectByExample(contractProductExample);
        //存入报关商品
        Map<String, String> map = new HashMap<>();
        if (contractProducts != null && contractProducts.size() > 0) {
            for (ContractProduct product : contractProducts) {
                ExportProduct exportProduct = new ExportProduct();
                export.setId(UuidUtil.getUuid());
                //拷贝数据
                BeanUtils.copyProperties(product, exportProduct);
                exportProduct.setExportId(export.getId());
                exportProductDao.insertSelective(exportProduct);
                map.put(product.getId(), exportProduct.getId());
            }
        }
        //查询附件
        ExtCproductExample extCproductExample = new ExtCproductExample();
        ExtCproductExample.Criteria extCproductExampleCriteria = extCproductExample.createCriteria();
        extCproductExampleCriteria.andContractIdIn(Arrays.asList(contractIds));
        List<ExtCproduct> extCproducts = extCproductDao.selectByExample(extCproductExample);
        if (extCproducts != null && extCproducts.size() > 0) {
            for (ExtCproduct extCproduct : extCproducts) {
                ExtEproduct extEproduct = new ExtEproduct();
                //拷贝附件对象
                BeanUtils.copyProperties(extCproduct, extEproduct);
                extEproduct.setId(UuidUtil.getUuid());
                extEproduct.setExportId(export.getId());
                extEproduct.setExportProductId(map.get(extCproduct.getContractProductId()));
                //存入附件商品
                extEproductDao.insertSelective(extEproduct);
            }
        }
        //设置运单属性
        export.setProNum(contractProducts == null ? 0 : contractProducts.size());
        export.setExtNum(extCproducts == null ? 0 : extCproducts.size());

        exportDao.insertSelective(export);
    }

    @Override
    public void update(Export export) {
        exportDao.updateByPrimaryKeySelective(export);
        List<ExportProduct> exportProductList = export.getExportProducts();
        if (exportProductList != null && exportProductList.size() > 0) {
            for (ExportProduct exportProduct : exportProductList) {
                exportProductDao.updateByPrimaryKeySelective(exportProduct);
            }
        }
        //修改原来购销合同的状态为1
        String[] array = export.getContractIds().split(",");
        for (String contractId : array) {
            Contract contract = contractDao.selectByPrimaryKey(contractId);
            contract.setState(1);
            //更新原数据合同
            contractDao.updateByPrimaryKeySelective(contract);
        }

    }

    @Override
    public void delete(String id) {
        //删除报关
        exportDao.deleteByPrimaryKey(id);
        //删除报关商品
        exportProductDao.deleteByExportId(id);
        //删除报关附件
        extEproductDao.deleteByExportId(id);
    }

    @Override
    public void submitState(String id) {
        Export export = exportDao.selectByPrimaryKey(id);
        export.setState(1L);
        exportDao.updateByPrimaryKeySelective(export);
    }

    @Override
    public void cancelState(String id) {
        Export export = exportDao.selectByPrimaryKey(id);
        export.setState(0L);
        exportDao.updateByPrimaryKeySelective(export);
    }
}
